package br.domsd.pocketcontrol;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import br.domsd.pocketcontrol.action.MainActivityActionListener;
import br.domsd.pocketcontrol.action.MonthExpensesListViewActionListener;
import br.domsd.pocketcontrol.utils.Utils;

public class MainActivity extends Activity
{

	MainActivityActionListener actionListener;
	MonthExpensesListViewActionListener listViewActionListener;
	ArrayAdapter<String> monthExpensesListViewAdapter;
	
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
    	actionListener = new MainActivityActionListener(this);
    	listViewActionListener = new MonthExpensesListViewActionListener(this);
    	
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        Button btnCreateXML = (Button) findViewById(R.id.btnCreateXML);
        btnCreateXML.setOnClickListener(actionListener);
        
        Button btnReadXML = (Button) findViewById(R.id.btnReadXML);
        btnReadXML.setOnClickListener(actionListener);
        
        ArrayList<String> monthExpenses = Utils.checkMonthExpenses(getFilesDir());
        
        
        monthExpensesListViewAdapter = new ArrayAdapter<String>(this, 
                android.R.layout.simple_list_item_1, monthExpenses);
        
        TextView tv = new TextView(this);
        tv.setText("Meses");
        
        ListView monthExpensesListView = (ListView) findViewById(R.id.monthExpenses);
        
        monthExpensesListView.addHeaderView(tv);
        monthExpensesListView.setHeaderDividersEnabled(true);
        monthExpensesListView.setAdapter(monthExpensesListViewAdapter);
        monthExpensesListView.setOnItemClickListener(listViewActionListener);
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    public void updateMonthExpensesListView()
    {
    	ArrayList<String> documents = Utils.checkMonthExpenses(getFilesDir());
    	
    	monthExpensesListViewAdapter = new ArrayAdapter<String>(this, 
                android.R.layout.simple_list_item_1, documents);
    	
    	ListView monthExpenses = (ListView) findViewById(R.id.monthExpenses);
    	monthExpenses.setAdapter(monthExpensesListViewAdapter);
    }
    
    public String getSelectedMonth()
    {
    	Spinner spinner = (Spinner) findViewById(R.id.spinnerMonth);
    	String selectedMonth = spinner.getSelectedItem().toString();
    	
    	return selectedMonth;
    }
    
    public String getSelectedYear()
    {
    	Spinner spinner = (Spinner) findViewById(R.id.spinnerYear);
    	String selectedYear= spinner.getSelectedItem().toString();
    	
    	return selectedYear;
    }
    
}
